package mipo.top.kaoshi.controller;

import mipo.top.base.annotation.PerAuthorize;
import mipo.top.base.util.JsonResult;
import mipo.top.base.util.PageUtil;
import mipo.top.kaoshi.domain.DictionaryDetail;
import mipo.top.kaoshi.query.DictionaryDetailQuery;
import mipo.top.kaoshi.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dicdetail")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping
    public List<DictionaryDetail> sad(){
        return dictionaryDetailService.findall();
    }
    @GetMapping("/{id}")
    public List<DictionaryDetail> osd(@PathVariable("id") Long id){
        return dictionaryDetailService.findTypesId(id);
    }
    @PutMapping
    public JsonResult su(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if(dictionaryDetail.getId()==null){
                dictionaryDetailService.save(dictionaryDetail);
            }else{
                dictionaryDetailService.update(dictionaryDetail);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }

    @DeleteMapping("/{id}")
    public void adw(@PathVariable("id") Long id){
        dictionaryDetailService.remove(id);
    }

    @DeleteMapping("/types/{id}")
    @PerAuthorize(name = "数据字典删除",sn = "Dic:Del")
    public void wsd(@PathVariable("id") Long id){
        dictionaryDetailService.deleteTypes(id);
    }

    @PostMapping
    @PerAuthorize(name = "数据字典分页查询",sn = "Dic:Query")
    public PageUtil<DictionaryDetail> asp(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.queryData(dictionaryDetailQuery);
    }
    @PatchMapping
    @PerAuthorize(name = "数据字典批量删除",sn = "Dic:bacDel")
    private void bacthDel(@RequestBody List<Long> ids){
        dictionaryDetailService.patchremove(ids);
    }
}
